package com.tianbing.adlottery.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.User;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyProfileController.class.getName());

	private ICategoryDao categoryDao;

	private IUserDao userDao;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		User user = userDao.findUserBySysId(auth.getUserSysId());
		fb.setUser(user);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession authorization = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		User user = form.getUser();

		if (form.getPassword() != null && !form.getPassword().trim().equals("") && !isDemoUser(authorization)) {
			// refer to PasswordEditValidator,here will be executed only passed
			// PasswordEditValidator
			user.setPassword(form.getNewPassword());
		}
		user = userDao.saveUser(user);

		skinUtils.setUserAuthorization(request, response, user.getUserId(), user.getPassword(), true);
		// partyService.savePartyDetail(ModelRequest.newRequest().setParty(oldPerson));
		mv.getModel().put("tips", "Your password has been changed successfully. " + "form.getNewConfirmPassword()");
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);

		String ptgid = ServletRequestUtils.getStringParameter(request, "ptgid", IConstants.CategoryID.AREA_TOP.getId());
		List categoryTrail = categoryDao.getListCategoryPrimaryTrailBy(ptgid);
		List categorySubGroups = categoryDao.getListChildCategoryBy(ptgid, 0, 100, null);
		model.put("categoryTrail", categoryTrail);
		model.put("categorySubGroups", categorySubGroups);
		return model;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	private boolean isDemoUser(UserSession authorization) {
		if (authorization != null && authorization.getUserId() != null
				&& authorization.getUserId().equals("demo@500hao.com"))
			return true;
		return false;
	}
}
